%Generate table 3, 4,  A3, A4  and A5 and credibility series appearing in
%figure 3

clear all

addpath 'FAVAR functions'

MacComputer = 1; %Set = 1 if running on a Mac, = 0 if running on a PC

if MacComputer == 1
    slash = '/';
else
    slash = '\';
end




gen_mats; %import data from excel write into data_small.mat

nrep1 = 10;  %number of repetitions in bootstrap for factor stage (10 for testing, 100 to generate tables)
nrep2 = 10;   %number of repetitions in bootstrap for VAR  (10 for testing, 100 to generate tables)
bootstrap = 1; %=1 if bootstraps are run, 0 otehrwise
nlags_VAR = 10; % number of lags in VAR = 5 in benchmark. Change to 10 in order to turn table 4 into A4.

load(['data',slash,'data_small.mat'])

year = data.year;
month = data.month;


%Pick event dates
tss{1} = [find(year==1896 & month==12)];  %Only 1896 election
tss{2} = [find(year==1896 & month==12) find(year==1907 & month==12)]; %control for 1907 panic
tss{3} = [find(year==1896 & month==12) find(year==1888 & month==12)]; %control for 1888 election
tss{4} = [find(year==1896 & month==12) find(year==1892 & month==12)]; %control for 1892 election
tss{5} = [find(year==1896 & month==12) find(year==1900 & month==12)]; %control for 1900 election
tss{6} = [find(year==1896 & month==12) find(year==1897 & month==7)]; %control for Alaska gold find
tss{7} = [find(year==1896 & month==12) find(year==1888 & month==10)]; %control for Commodity shocks
tss{8} =  [find(year==1896 & month==12) [find(year==1907 & month==12)+[-2:2]]]; %control for 1907 panic with window
tss{9} =  [find(year==1896 & month==12) [find(year==1897 & month==7)+[-2:2]]]; %control for Alaska gold find with window
tss{10} = [find(year==1896 & month==12) find(year==1907 & month==12) find(year==1888 & month==12)]; %control for 1907 panic
tss{11} = [find(year==1896 & month==12) [find(year==1888 & month==10)+[-2:2]]]; %control for Commodity shocks with window
tss{12} = [find(year==1896 & month==12) find(year==1901 & month==9)]; %control for Teddy Roosevelt's  populism
tss{13} = [find(year==1895 & month==7)]; %alternative date
line = 1;
table = [];

parbench = {tss{1},11,0,0,nlags_VAR,0,2}; %benchmark parametrization
%parbench = (1) tss, (2) factors (0 factors = high low factor), (3) normalization, (4) xlags, (5) lags, (6)  Flags, (7) order


ybench = 2; %Aggregate activity index: y=2: bus. failures / y =3: pig iron / y =1: factory employment / y=4: industrial production / y=5: industrial production smoothed : 

nparamets = 20;

Fst0s = zeros(size(x,1),8);

%for i = 1:nparamets
 
for i = 1:21
    load(['data',slash,'data_small.mat'])

    
    
    par=parbench;
    y = ybench;
    
    
switch i 

    
    case 2 %5 factors
        par{2} = 5;
    case 3 % 20 factors
        par{2} = 20;
    case 4 % control for 1907 panic
        par{1} = tss{2};
    case 5 % control for 1888 election
        par{1}=tss{3};
    case 6 % control for 1900 election
        par{1} = tss{5};
    case 7 % control for commodity shock
        par{1} = tss{7};
    case 8 % control 1907 panic (with window)
        par{1} = tss{8};
    case 9 % control for commodity shock (with window)
        par{1} = tss{11};
    case 10 % change order of cholesky decomposition
        par{7} = 1;
    case 11 %use pig iron
        y = 3;
    case 12 %change order and use pig iron
        y = 3;
        par{7} = 1;
    case 13 %use factory employment
        y = 1; 
    case 14 %change order and use factory employment
        y = 1;
        par{7}=1;
    case 15 %use industrial production
        y = 4; 
    case 16 %change order and use industrial production 
        y = 4;
        par{7}=1; 
    case 17 % use 5 lags in F
        par{6} = 5;
    case 18 %control for Teddy Rossevelt's populism
        par{1} = tss{12};
    case 19 % industrial production (smoothed)
        y = 5; 
    case 20 %change order and industrial production (smoothed)
         y = 5;
        par{7}=1; 
  case 21 %alternative date
         y = 5;
        par{1}=tss{13}; 

        
        
        
end
    
datay = data;
datay.y = data.y(:,[y,end-1:end]);

out = bootstrap_tables(datay,par{:},bootstrap,nrep1,nrep2);
   
y_counterfactual{i} = out.y(:,1);


if i == 1
    Fst0bench = out.Fst0;
end


out.tab(abs(out.tab)>1000)=NaN;


    table = [table;out.tab];
    
    
    
    
Fst0s(:,i) = out.Fst0(:,1);    
    
    
    line = line+1
end
 
 


%Table 3
cols = [13,1,2,3,4];
rows = [1,4,5,6,7,18]; 
for k = 1:length(rows)
    for m = 1:length(cols)
    tabc_3{2*(k-1)+1,m} = num2str(table(3*(rows(k)-1)+2,cols(m)),3);
    tabc_3{2*k,m} = ['(',num2str(table(3*(rows(k)-1)+1,cols(m)),3),',',num2str(table(3*rows(k),cols(m)),3),')'];
    end
end


%Table 4 (or A4 if nlags_VAR = 10)
cols = [9,10,7,6];
rows = [1,10:16,19:20];
for k = 1:length(rows)
    for m = 1:length(cols)
    tabc_4{2*(k-1)+1,m} = num2str(table(3*(rows(k)-1)+2,cols(m)),3);
    tabc_4{2*k,m} = ['(',num2str(table(3*(rows(k)-1)+1,cols(m)),3),',',num2str(table(3*rows(k),cols(m)),3),')'];
    end
end




%Table A3
cols = [13,1,2,3,4];
rows = [1,2,3,8,9]; 
for k = 1:length(rows)
    for m = 1:length(cols)
    tabc_A3{2*(k-1)+1,m} = num2str(table(3*(rows(k)-1)+2,cols(m)),3);
    tabc_A3{2*k,m} = ['(',num2str(table(3*(rows(k)-1)+1,cols(m)),3),',',num2str(table(3*rows(k),cols(m)),3),')'];
    end
end




%Table A5
cols = [11,12,8,6];
rows = [1,10:16,19:20];
for k = 1:length(rows)
    for m = 1:length(cols)
    tabc_A5{2*(k-1)+1,m} = num2str(table(3*(rows(k)-1)+2,cols(m)),3);
    tabc_A5{2*k,m} = ['(',num2str(table(3*(rows(k)-1)+1,cols(m)),3),',',num2str(table(3*rows(k),cols(m)),3),')'];
    end
end


%Generate indicator series and confidence bands 

F1s = out.F1s;
l_ci_Fs = ceil(0.05*size(F1s,2));
u_ci_Fs = floor(0.95*size(F1s,2));
cumFs = cumsum(F1s);
cumFssort = sort(cumFs,2);

F1sband = [cumFssort(:,l_ci_Fs),mean(cumFssort,2),cumFssort(:,u_ci_Fs)]; %use these three series for figure 6
%save results



